﻿using System;
using Microsoft.DirectX.AudioVideoPlayback;
using System.IO;
using AudioPlayer.Plugin.Playback;
namespace Playback.DirectX {
	public class Playback : IPlayback {
		#region Variables
		private Audio _audio;
		private Video _video;
		#endregion
		#region Constructor
		public Playback() {
			
		}
		#endregion
		#region Private Voids
		private void SubscribeAudioEvents() {
			_audio.Ending += new EventHandler(Audio_Ending);
		}
		#endregion
		#region Public Voids
		public void Play() {
			if (_audio != null) {
				_audio.Play();
				this.OnStarting();
			}
		}
		public void Load(string url) {
			if (File.Exists(url)) {
				try {
					if (_audio == null) {
						_audio = new Audio(url);
						this.SubscribeAudioEvents();
					} else {
						_audio.Open(url);
					}
				} catch {

				}
			}
		}
		public void Load(String url, IntPtr hwnd) {
			try {
				
			} catch { }
		}
		public void Pause() {
			if (Ready) {
				if (_audio.Playing) {
					try {
						_audio.Pause();
					} catch {
						
					}
				}
			}
		}
		public void Stop() {
			if (Ready) {
				if (_audio.Playing) {
					try {
						_audio.Stop();
					} catch {
						
					}
				}
			}
		}
		public void Seek(int position, System.IO.SeekOrigin org) {
			if (Ready) {
				//double posPerc = this._audio.Duration * position*100000;
				double posPerc = (_audio.StopPosition * position) / 100;
				_audio.SeekCurrentPosition(posPerc, SeekPositionFlags.AbsolutePositioning | SeekPositionFlags.NoFlush);
			}
		}
		#endregion
		#region Events
		/// <summary>
		/// Occurs when playback starts.
		/// </summary>
		public event EventHandler Starting;
		/// <summary>
		/// Occurs when playback ends.
		/// </summary>
		public event EventHandler Ending;

		protected virtual void OnStarting() {
			if (Starting != null) {
				try {
					this.Starting((IPlayback)this, EventArgs.Empty);
				} catch { }
			}
		}
		protected virtual void OnEnding() {
			if (Ending != null) {
				try {
					this.Ending((IPlayback)this, EventArgs.Empty);
				} catch { }
			}
		}
		#endregion
		#region Event Handling
		private void Audio_Ending(object sender, EventArgs e) {
			this.OnEnding();
		}
		#endregion
		#region Property
		public string Author {
			get { return "Vitalijus Jefisovas"; }
		}
		public string URL {
			get { return ""; }
		}
		public string Title {
			get { return "DirectX Backend"; }
		}
		public string Description {
			get { return "Playes audio/video files, through directX"; }
		}
		public string Version {
			get { return "0.0.0.1"; }
		}

		public bool CanPlay {
			get {
				if (Ready) {
					return true;
				} else {
					return false;
				}
			}
		}
		public bool CanSeek {
			get {
				if (Ready) {
					if (_audio.Playing) {
						return _audio.SeekingCaps.CanSeekAbsolute;
					}
				}
				return false;
			}
		}
		public bool CanPause {
			get { return true; }
		}
		public bool CanChangeVolume {
			get {
				return true;
			}
		}
		public bool CanChangeBalance {
			get {
				return true;
			}
		}
		public bool Ready {
			get {
				if (_audio != null) {
					return true;
				} else {
					return false;
				}
			}
		}
		public bool IsPlaying {
			get {
				if (Ready){
					try {
						return _audio.Playing;
					} catch { return false; }
				}
				return false;
			}
		}
		public int Volume {
			get {
				if (Ready) {
					return _audio.Volume;
				}
				return 0;
			}
			set {
				if (Ready) {
					_audio.Volume = value;
				}
			}
		}
		public int Balance {
			get {
				return _audio.Balance;
			}
			set {
				_audio.Balance = value;
			}
		}
		public double Duration {
			get {
				if (Ready && _audio.Playing) {
					try {
						return _audio.Duration;
					} catch {
						return 0;
					}
				}
				return 0;
			}
		}
		public double Current {
			get {
				if (this.IsPlaying) {
					try {
						return _audio.CurrentPosition;
					} catch { return 0; }
				}
				return 0;
			}
		}
		#endregion

		#region IDisposable Members

		public void Dispose() {
			//_audio.Dispose();
		}

		#endregion
	}
}
